

var ws=require("nodejs-websocket");
var port=888;
var clientCount=0;
 cs=function(req,res){
	console.log('connect');
	clientCount++;
	req.nickname="user"+clientCount;
	broadcast(req.nickname+"comes in")
	req.on("text",function(str){
		req.sendText(str);

	});
	req.on("close",function(code,reason){
		broadcast(req.nickname+"left")
		console.log("server is close")
	})
	req.on("error",function(err){
       console.log("handle err");
       console.log(err)
	})
	console.log('server is ok')

}
function broadcast(str){
   server.connections.forEach(function(connection){
        connection.sendText(str)
})
}
var server=ws.createServer(cs).listen(port)